<?php

namespace addons\mental\controller;

use think\Db;


class Collect extends Base
{
    protected $noNeedLogin = [];
    
    
    /**
     * 添加收藏(addons/mental/collect/save)
     * @param int $main_id 量表id
     */
    public function save()
    {
        $uid = $this->auth->id;
        $main_id = $this->request->param('main_id');
        
        $isData = Db::name('mental_collect')->where(['user_id'=>$uid,'main_id'=>$main_id])->find();
        if ($isData) {
            $this->error('量表已收藏');
        }
        
        $ret = Db::name('mental_collect')->insert(['user_id'=>$uid,'main_id'=>$main_id]);
        if ($ret) {
            $this->success('收藏成功');
        }else {
            $this->error('收藏失败');
        }
    }
    
    /**
     * 取消收藏(addons/mental/collect/del)
     * @param int $main_id 量表id
     */
    public function del()
    {
        $uid = $this->auth->id;
        $main_id = $this->request->param('main_id');
        
        $isData = Db::name('mental_collect')->where(['user_id'=>$uid,'main_id'=>$main_id])->find();
        if (!$isData) {
            $this->error('量表已取消收藏');
        }
        
        $ret = Db::name('mental_collect')->where(['id'=>$isData['id']])->delete();
        if ($ret) {
            $this->success('收藏取消成功');
        }else {
            $this->error('取消失败');
        }
    }
    
    
    /**
     * 收藏列表(addons/mental/collect/lists)
     */
    public function lists()
    {
        $uid = $this->auth->id;
        $list = Db::name('mental_collect')
            ->alias('c')
            ->join('mental_testmain t','t.id=c.main_id')
            ->where(['c.user_id'=>$uid])
            ->field("t.id,t.type,t.title,t.description,t.price,t.image,t.test_num,t.pay_mode,t.ex1,t.ex2,t.ex3")
            ->paginate(15);
        
        $this->success('success',$list);
    }
    
}
